﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using DocumentFormat.OpenXml.Spreadsheet;

namespace OPENXMLLib
{
    public class OpenXmlDoc : IDisposable
    {
        private Dictionary<string, XSheet> _xSheetDict = new Dictionary<string, XSheet>();

        public SpreadsheetDocument ExcelDoc { get; private set; }
        Stream _stream;

        public OpenXmlDoc(string filePath)
        {
            _stream = new FileStream(filePath, FileMode.Open);

            OpenSettings settings = new OpenSettings();
            settings.AutoSave = true;
            ExcelDoc = SpreadsheetDocument.Open(_stream, true, settings);
        }

        public void Dispose()
        {
            _stream.Close();
        }

        public List<string> GetSheets()
        {
            return ExcelDoc.WorkbookPart.Workbook.Elements<Sheets>().SingleOrDefault().Elements<Sheet>().Select(p => p.Name.Value).ToList();
        }

        public void Save()
        {
            //_doc.s
        }
    }
}
